package org.jetlinks.community.mstn.web.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.hswebframework.ezorm.rdb.mapping.annotation.Comment;
import org.hswebframework.web.authorization.Dimension;
import org.hswebframework.web.system.authorization.api.entity.AuthorizationSettingEntity;
import org.jetlinks.community.mstn.entity.AssetsEntity;

import javax.swing.text.html.parser.Entity;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Getter
@Setter
public class AssetsBindDeviceRequest {

    @Schema(description = "资产id列表")
    @Comment("资产id列表")
    private List<String> assetIdList;

    @Schema(description = "资产类型")
    @Comment("资产类型")
    private String assetType;

    @Schema(description = "权限")
    @Comment("权限")
    private Set<String > permission;

    @Schema(description = "其他配置")
    @Comment("其他配置")
    private Map<String, Object> properties;

    @Schema(description = "资产目标,如: ID")
    @Comment("资产目标")
    private String targetId;

    @Schema(description = "绑定类型")
    @Comment("绑定类型")
    private String targetType;

    /**
     * 冲突时是否合并
     */
    @Schema(description = "冲突时是否合并")
    private boolean merge = true;

    /**
     * 冲突时优先级
     */
    @Schema(description = "冲突时合并优先级")
    private int priority = 10;

    public List<AssetsEntity> toEntity() {
        return toEntity(null);
    }

    public List<AssetsEntity> toEntity(Dimension dimension) {
        if (CollectionUtils.isEmpty(assetIdList)) {
            return Collections.emptyList();
        }
        if (CollectionUtils.isEmpty(permission)) {
            return Collections.emptyList();
        }
        return assetIdList
            .stream()
            .map(device -> {
                AssetsEntity entity = new AssetsEntity();
                entity.setAssetId(device);
                entity.setAssetType(assetType);
                entity.setAssetTarget(targetId);
                entity.setAssetTargetType(getTargetType());

                if (null != dimension) {
                    entity.setAssetTypeName(dimension.getType().getName());
                    entity.setAssetTargetName(dimension.getName());
                }

                entity.setState((byte) 1);
                entity.setPermission(permission);
                entity.setProperties(properties);
                entity.setMerge(merge);
                entity.setPriority(priority);
//                permissionInfo.wrap(entity);
                return entity;
            }).collect(Collectors.toList());
    }
}
